package com.engine.qz.util;

import com.alibaba.fastjson.JSONObject;
import com.api.browser.bean.BrowserBean;
import com.api.browser.bean.SearchConditionItem;
import com.api.browser.bean.SearchConditionOption;
import com.api.browser.util.ConditionFactory;
import com.api.browser.util.ConditionType;
import com.engine.qz.entity.browser.po.CustomBrowserBean;
import weaver.hrm.User;
import weaver.systeminfo.SystemEnv;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author dxfeng
 * @description:
 * @Date 2022/5/9
 * @Version V1.0
 **/
public class SearchItemUtil {

    /**
     * 下拉框
     *
     * @param user
     * @param selectOptions
     * @param colSpan
     * @param fieldcol
     * @param labelcol
     * @param isQuickSearch
     * @param label
     * @return
     */
    public static SearchConditionItem selectItem(User user, List<SearchConditionOption> selectOptions, int colSpan, int fieldcol,
                                                 int labelcol, boolean isQuickSearch, String label, String name) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem select = conditionFactory.createCondition(ConditionType.SELECT, 502327, name);
        select.setOptions(selectOptions);
        select.setColSpan(colSpan);
        select.setFieldcol(fieldcol);
        select.setLabelcol(labelcol);
        select.setIsQuickSearch(isQuickSearch);
        select.setLabel(label);
        return select;
    }

    public static SearchConditionItem selectItem(User user, List<SearchConditionOption> selectOptions, int colSpan, int fieldcol,
                                                 int labelcol, boolean isQuickSearch, int label, String name) {
        return selectItem(user, selectOptions, colSpan, fieldcol, labelcol, isQuickSearch, SystemEnv.getHtmlLabelName(label, user.getLanguage()), name);
    }


    /**
     * checkbox
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param viewAttr
     * @param isQuickSearch
     * @param label
     * @param name
     * @return
     */
    public static SearchConditionItem checkboxItem(User user, int colSpan, int fieldcol,
                                                   int viewAttr, boolean isQuickSearch, String label, String name) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem checkbox = conditionFactory.createCondition(ConditionType.CHECKBOX, 502327, name);
        checkbox.setColSpan(colSpan);
        checkbox.setFieldcol(fieldcol);
        checkbox.setViewAttr(viewAttr);
        checkbox.setIsQuickSearch(isQuickSearch);
        checkbox.setLabel(label);
        return checkbox;
    }


    /**
     * 输入框数字
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param viewAttr
     * @param label
     * @return
     */
    public static SearchConditionItem inputNumberItem(User user, int colSpan, int fieldcol,
                                                      int viewAttr, String label, String name) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem inputnumber = conditionFactory.createCondition(ConditionType.INPUTNUMBER, 502327, name);
        inputnumber.setColSpan(colSpan);
        inputnumber.setFieldcol(fieldcol);
        inputnumber.setViewAttr(viewAttr);
        inputnumber.setLabel(label);
        return inputnumber;
    }

    public static SearchConditionItem inputNumberItem(User user, int colSpan, int fieldcol,
                                                      int viewAttr, int label, String name) {
        return inputNumberItem(user, colSpan, fieldcol, viewAttr, SystemEnv.getHtmlLabelName(label, user.getLanguage()), name);
    }


    /**
     * 输入框文本
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param viewAttr
     * @param length
     * @param label
     * @return
     */
    public static SearchConditionItem inputItem(User user, int colSpan, int fieldcol,
                                                int viewAttr, int length, String label, String name) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        Map<String, Object> otherParams = new HashMap<>(2);
        otherParams.put("inputType", "multilang");
        SearchConditionItem input = conditionFactory.createCondition(ConditionType.INPUT, 25034, name);
        input.setColSpan(colSpan);
        input.setFieldcol(fieldcol);
        input.setViewAttr(viewAttr);
        input.setLength(length);
        input.setLabel(label);
        input.setOtherParams(otherParams);
        return input;
    }

    public static SearchConditionItem inputItem(User user, int colSpan, int fieldcol,
                                                int viewAttr, int length, String label, String name, String tip) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        Map<String, Object> otherParams = new HashMap<>(2);
        otherParams.put("inputType", "multilang");
        SearchConditionItem input = conditionFactory.createCondition(ConditionType.INPUT, 25034, name);
        input.setColSpan(colSpan);
        input.setFieldcol(fieldcol);
        input.setViewAttr(viewAttr);
        input.setLength(length);
        input.setLabel(label);
        input.setOtherParams(otherParams);
        input.setTip(tip);
        return input;
    }

    public static SearchConditionItem inputItem(User user, int colSpan, int fieldcol,
                                                int viewAttr, int length, int label, String name) {
        return inputItem(user, colSpan, fieldcol, viewAttr, length, SystemEnv.getHtmlLabelName(label, user.getLanguage()), name);
    }


    /**
     * 浏览按钮
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param viewAttr
     * @param isQuickSearch
     * @param label
     * @param type
     * @param name
     * @param fieldDbType   数据展现集成标识
     * @return
     */
    public static SearchConditionItem browserItem(User user, int colSpan, int fieldcol,
                                                  int viewAttr, boolean isQuickSearch, String label, String type, String name, String fieldDbType) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem browser = conditionFactory.createCondition(ConditionType.BROWSER, 502327, name, type);
        browser.setColSpan(colSpan);
        browser.setFieldcol(fieldcol);
        browser.setViewAttr(viewAttr);
        browser.setIsQuickSearch(isQuickSearch);
        browser.setLabel(label);
        //隐藏虚拟组织
        browser.getBrowserConditionParam().setHideVirtualOrg(true);
        if ("161".equals(type) || "162".equals(type)) {
            fieldDbType = "browser." + fieldDbType;
            BrowserBean browserBean = new BrowserBean();
            // completeParams
            Map<String, Object> completeMap = new HashMap<>();
            completeMap.put("f_weaver_belongto_usertype", 0);
            completeMap.put("type", type);
            completeMap.put("fielddbtype", fieldDbType);
            browserBean.setCompleteParams(completeMap);
            // conditionDataParams
            Map<String, Object> conditionDataMap = new HashMap<>();
            conditionDataMap.put("f_weaver_belongto_usertype", 0);
            conditionDataMap.put("type", fieldDbType);
            conditionDataMap.put("fielddbtype", fieldDbType);
            browserBean.setConditionDataParams(conditionDataMap);
            // dataParams
            Map<String, Object> dataMap = new HashMap<>();
            dataMap.put("f_weaver_belongto_usertype", 0);
            dataMap.put("type", fieldDbType);
            browserBean.setDataParams(dataMap);
            // destDataParams
            Map<String, Object> destDataMap = new HashMap<>();
            destDataMap.put("f_weaver_belongto_usertype", 0);
            destDataMap.put("type", fieldDbType);
            browserBean.setDestDataParams(destDataMap);

            browserBean.setLinkUrl("");
            browserBean.setMobileLinkUrl("");
            browserBean.setPageSize(10);
            browserBean.setScrollx(true);
            browserBean.setTitle(label);
            browserBean.setType(type);

            browser.setBrowserConditionParam(browserBean);
            if ("162".equals(type)) {
                HashMap<String, Object> otherMap = new HashMap<>();
                otherMap.put("isMultCheckbox", "true");
                otherMap.put("icon", "icon-coms-integration");
                browser.setOtherParams(otherMap);
            }
        }
        if ("666".equals(type)) {
            BrowserBean browserBean = browser.getBrowserConditionParam();
            String s = JSONObject.toJSONString(browserBean);
            CustomBrowserBean customBrowserBean = JSONObject.parseObject(s, CustomBrowserBean.class);
            customBrowserBean.setHasLeftTree(true);
            customBrowserBean.setLeftToSearchKey("treeKey");

            Map<String, Object> map = new HashMap<>(2);
            map.put("width", 988);
            customBrowserBean.setModalStyle(map);
            browser.setBrowserConditionParam(customBrowserBean);
        }
        return browser;
    }

    public static SearchConditionItem browserItem(User user, int colSpan, int fieldcol,
                                                  int viewAttr, boolean isQuickSearch, int label, String type, String name, String fieldDbType) {
        return browserItem(user, colSpan, fieldcol, viewAttr, isQuickSearch, SystemEnv.getHtmlLabelName(label, user.getLanguage()), type, name, fieldDbType);
    }

    /**
     * 日期区间
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param viewAttr
     * @param label
     * @return
     */
    public static SearchConditionItem rangeDateInterval(User user, int colSpan, int fieldcol, int viewAttr
            , String label, String name1, String name2) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem rangeDate = conditionFactory.createCondition(ConditionType.DATE_INTERVAL, 0, new String[]{name1, name2});
        rangeDate.setColSpan(colSpan);
        rangeDate.setFieldcol(fieldcol);
        rangeDate.setViewAttr(viewAttr);
        rangeDate.setLabel(label);
        return rangeDate;
    }


    public static SearchConditionItem rangeDateItem(User user, int colSpan, int fieldcol, int viewAttr, String label, String name1) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem rangeDate = conditionFactory.createCondition(ConditionType.DATEPICKER, 502327, new String[]{name1});
        rangeDate.setColSpan(colSpan);
        rangeDate.setFieldcol(fieldcol);
        rangeDate.setViewAttr(viewAttr);
        rangeDate.setLabel(label);
        return rangeDate;
    }


    /**
     * 多行文本框
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param isQuickSearch
     * @param viewAttr
     * @param length
     * @param label
     * @param name
     * @return
     */
    public static SearchConditionItem textareaItem(User user, int colSpan, int fieldcol, boolean isQuickSearch,
                                                   int viewAttr, int length, String label, String name) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem textarea = conditionFactory.createCondition(ConditionType.TEXTAREA, 502227, name);
        textarea.setColSpan(colSpan);
        textarea.setFieldcol(fieldcol);
        textarea.setIsQuickSearch(isQuickSearch);
        textarea.setViewAttr(viewAttr);
        textarea.setLength(length);
        textarea.setLabel(label);
        textarea.setInputType("multilang");
        return textarea;
    }

    public static SearchConditionItem textareaItem(User user, int colSpan, int fieldcol, boolean isQuickSearch,
                                                   int viewAttr, int length, int label, String name) {
        return textareaItem(user, colSpan, fieldcol, isQuickSearch, viewAttr, length, SystemEnv.getHtmlLabelName(label, user.getLanguage()), name);
    }


    /**
     * 日期组合
     *
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param isQuickSearch
     * @param viewAttr
     * @param label
     * @param name
     * @return
     */
    public static SearchConditionItem dateItem(User user, int colSpan, int fieldcol, boolean isQuickSearch,
                                               int viewAttr, String label, String name) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        SearchConditionItem date = conditionFactory.createCondition(ConditionType.DATE, 502227, name);
        date.setColSpan(colSpan);
        date.setFieldcol(fieldcol);
        date.setIsQuickSearch(isQuickSearch);
        date.setViewAttr(viewAttr);
        date.setLabel(label);
        return date;
    }

    public static SearchConditionItem dateItem(User user, int colSpan, int fieldcol, boolean isQuickSearch,
                                               int viewAttr, int label, String name) {
        return dateItem(user, colSpan, fieldcol, isQuickSearch, viewAttr, SystemEnv.getHtmlLabelName(label, user.getLanguage()), name);
    }

    /**
     * @param user
     * @param colSpan
     * @param fieldcol
     * @param isQuickSearch
     * @param viewAttr
     * @param label
     * @param name
     * @return
     */
    public static SearchConditionItem datePickerItem(User user, int colSpan, int fieldcol, boolean isQuickSearch,
                                                     int viewAttr, String label, String name1, String name2) {
        ConditionFactory conditionFactory = new ConditionFactory(user);
        String[] domkey = new String[]{name1, name2};
        SearchConditionItem datePicker = conditionFactory.createCondition(ConditionType.RANGEPICKER, 502327, domkey);
        datePicker.setColSpan(colSpan);
        datePicker.setFieldcol(fieldcol);
        datePicker.setViewAttr(viewAttr);
        datePicker.setLabel(label);
        return datePicker;
    }

    public static SearchConditionItem datePickerItem(User user, int colSpan, int fieldcol, boolean isQuickSearch,
                                                     int viewAttr, int label, String name1, String name2) {
        return datePickerItem(user, colSpan, fieldcol, isQuickSearch, viewAttr, SystemEnv.getHtmlLabelName(label, user.getLanguage()), name1, name2);
    }
}
